package cn.hp.controller;

import cn.hp.entity.Menu;
import cn.hp.entity.User;
import cn.hp.service.IUserService;
import cn.hp.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController {

    @Autowired
    private IUserService userService;

    @RequestMapping("main")
    public String main(){
        return "main";
    }


    @GetMapping("getUser")
    @ResponseBody
    public Result getUser(HttpSession session){
        User user = (User) session.getAttribute("user");
        return Result.success(user);
    }



    @RequestMapping("logout")
    public String logout(HttpSession session){
        session.removeAttribute("user");
        return "login";
    }

    @PostMapping("dologin")
    @ResponseBody
    public Result doLogin(@RequestBody User user, HttpSession session){

        User user1 = userService.doLogin(user);
        System.out.println(user1);
        if (user1 != null){
            session.setAttribute("user",user1);
            return Result.success();
        }

        else {
            return Result.error("邮箱或密码有误");
        }


    }
    @GetMapping("getUidMenu")
    @ResponseBody
    public Result getUidMenu(HttpSession session) {
        // 从session中取出user数据
        User user = (User) session.getAttribute("user");
        // 获取user所拥有的权限
        List<Menu> list = userService.selectUidMenu(user);
        return Result.success(list);
    }
}
